﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DAL;
using Facade.Excel;

namespace PeisongWeb
{
    public partial class buyer_reportPayableDetails : buyerPage
    {
        protected int payid
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["payid"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["payid"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CB", 1))
                {
                    if (allowAccess(Convert.ToInt32(companyType.Buyer)))
                    {
                        if (Request.QueryString["pay"] != null)
                        {
                            if (Library.Number.IsIntPositive(Request.QueryString["pay"]))
                            {
                                payid = Convert.ToInt32(Request.QueryString["pay"]);
                            }
                        }

                        if (payid != 0)
                            rptBind();
                        else
                        {
                            Library.Script.ClientUrl("index.html");
                            return;
                        }

                    }
                    else
                    {
                        Library.Script.ClientUrl("index.html");
                        return;
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "buyer_index.html");
                    return;
                }
            }

        }
       
        void rptBind()
        {

            int company = logic.customer.getCompanyId();
            PaymentHistoryEntity _phe = logic.paymentHistory.getByPayId(payid);

            if (_phe.CompanyId == company) //判断是否为同一家公司
            {
                hid_startDt.Value = _phe.StartDt.ToString();
                hid_endDt.Value = _phe.EndDt.ToString();

                lbl_Scope.Text = _phe.StartDt.ToString() + " 至 " + _phe.EndDt.ToString();
                lbl_Amount.Text = string.Format("￥<a style='color:red;'>{0:F2}</a>", _phe.Amount);
                lbl_Status.Text = _phe.Status == "1" ? "未付" : "已付";


                string startDt = _phe.StartDt.ToString();
                string endDt = _phe.EndDt.ToString();
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("companyId={0} ", company);
                sb.AppendFormat(" and datediff(day,[updateStatusdt],'{0}')<=0", startDt);
                sb.AppendFormat(" and datediff(day,[updateStatusdt],'{0}')>=0", endDt);

                DataSet ds = logic.orderItem.listWithOrder(sb.ToString(), "oiid", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
                AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                rpt_list.DataSource = ds.Tables[1];
                rpt_list.DataBind();
            }
            else
            {
                Library.Script.ClientUrl("index.html");
                return;
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void lbtn_ToExecl_Click(object sender, EventArgs e)
        {
            int company = logic.customer.getCompanyId();
            string startDt = hid_startDt.Value;
            string endDt = hid_endDt.Value;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("companyId={0} ", company);
            sb.AppendFormat(" and datediff(day,[updateStatusdt],'{0}')<=0", startDt);
            sb.AppendFormat(" and datediff(day,[updateStatusdt],'{0}')>=0", endDt);


            DataSet ds = logic.orderItem.listwithReceivables(sb.ToString());
            DataTable dt=ds.Tables[0];
            dt.Columns["proName"].ColumnName = "商品";
            dt.Columns["quantity"].ColumnName = "数量";
            dt.Columns["price"].ColumnName = "单价";
            dt.Columns["unit"].ColumnName = "单位";
            dt.Columns["amount"].ColumnName = "小计";
            dt.Columns["orderNo"].ColumnName = "订单号";
            dt.Columns["statusName"].ColumnName = "订单状态";
            dt.Columns["updateStatusdt"].ColumnName = "成功日期";
            dt.Columns["receiveDt"].ColumnName = "收获日期";

            String xlsName = Convert.ToDateTime(startDt).ToShortDateString() + "至" + Convert.ToDateTime(endDt).ToShortDateString() + "的应付款";

            ExportFacade exportHelper = new ExportFacade();

            Dictionary<int, FormatStyle> formatOptions = new Dictionary<int, FormatStyle>();
            formatOptions.Add(1, FormatStyle.ToFix2);
            formatOptions.Add(2, FormatStyle.ToFix2);
            formatOptions.Add(4, FormatStyle.ToFix2);

            exportHelper.ExportByWeb(dt, xlsName, xlsName + ".xls", formatOptions,xlsName);
        }
    }
}
